<?php
/**
 * 订单接口处理
 *
 * @category 
 * @package 
 * @author chenping<chenping@shopex.cn>
 * @version $Id: Z
 */
class erpapi_shop_matrix_360buy_response_order extends erpapi_shop_response_order
{
    protected $_update_accept_dead_order = true;

    /**
     * 是否接收订单
     *
     * @return void
     * @author 
     **/
    protected function _canAccept()
    {
        if ($this->_ordersdf['store_order'] == '京仓订单') {
            $this->__apilog['result']['msg'] = '京仓订单不接收';

            return false;
        }

        // 商户类型不是SOP
        if ($this->__channelObj->channel['addon']['type'] != 'SOP') {
            $this->__apilog['result']['msg'] = '商户类型不是SOP订单不接收';

            return false;
        }

        if ($this->_ordersdf['t_type'] == 'fenxiao' || $this->_ordersdf['order_source'] == 'taofenxiao') {
            $this->__apilog['result']['msg'] = '分销订单暂时不接收';
            return false;
        }

        return parent::_canAccept();
    }

    protected function _analysis()
    {
        parent::_analysis();

        if(!$this->_ordersdf['lastmodify']){
            $this->_ordersdf['lastmodify'] = date('Y-m-d H:i:s',time());
        }

        $trade_refunding = false;
        //获取货号
        foreach ($this->_ordersdf['order_objects'] as &$object) {
            foreach ($object['order_items'] as &$item) {
                //货号不存在
                $sku   = array();
                if (empty($item['bn'])) {
                    $sku   = $this->item_get($item['shop_product_id']);
                    
                    if ($sku['sku'] && $sku['sku']['outer_id']) {
                        //货号
                        $item['bn']   = $sku['sku']['outer_id'];
                        $object['bn'] = $sku['sku']['outer_id'];
                    }
                }

                if ($item['status'] == 'refund') {
                    $trade_refunding = true;
                }
            }
        }

        if ($trade_refunding) {
            $this->_ordersdf['pay_status'] = '6';
        }

        if ($this->_ordersdf['status'] == 'finish' && $this->_ordersdf['ship_status'] == '0') $this->_ordersdf['status'] = 'active';
    }

    protected function _operationSel()
    {
        parent::_operationSel();

        if ($this->_tgOrder) $this->_operationSel = 'update';
    }

    protected function get_update_components()
    {
        $components = array('markmemo');

        if ( ($this->_ordersdf['pay_status'] != $this->_tgOrder['pay_status']) ||($this->_ordersdf['shipping']['is_cod']=='true' && $this->_ordersdf['status'] == 'dead') ) {
            $refundApply = app::get('ome')->model('refund_apply')->getList('apply_id',array('order_id'=>$this->_tgOrder['order_id'],'status|noequal'=>'3'));
            // 如果没有退款申请单，以前端为主
            if (!$refundApply) {
                $components[] = 'master';
            }
        }

        return $components;
    }


    protected function item_get($sku_id)
    {
        if (empty($sku_id)) {
            return array();
        }

        $rs = kernel::single('erpapi_router_request')->set('shop',$this->__channelObj->channel['shop_id'])->product_item_sku_get(array('sku_id'=>$sku_id));

        if ($rs['rsp'] == 'fail' || !$rs['data']) return array();

        return $rs['data'];
    }

    protected function _canUpdate()
    {
        if ( $this->_ordersdf['status'] == 'dead' && $this->_ordersdf['shipping']['is_cod'] != 'true') {
            $this->__apilog['result']['msg'] = '取消订单不接收';
            return false;
        }

        return parent::_canUpdate();
    }
}
